<?php
class OauthsController extends AppController {

	var $name = 'Oauths';
	var $uses = array('OauthServerRegistry');
	
	function beforeFilter() {
		parent::beforeFilter();	
		//$this->Auth->allow('*');
	}
	
	function isAuthorized(){
		if($this->Auth->user('group_id') == $this->Session->read('Settings.adminPermission')){		//admin jogosultságú júzerek
			return true;
		}
		//free user / premium user / business user
		if(in_array($this->action, array(
													'add', 'index', 'edit'
													))){
			return true;
		}
		return false;		//minden más tiltva

	}

/*
 * Register a new application
*/
	function add(){
		if($this->data){
			App::import('vendor', 'cakeOauthServiceProvider');
			
			//sanitize $this->data['Oauths']
			
			$this->data['Oauths']['requester_email'] = $this->Auth->user('email');
			$this->data['Oauths']['application_type'] = 'website';
			
			$oauth = new CakeOAuthServiceProvider();
			$consumer = $oauth->getConsumerKeyAndSecret(
								$this->Auth->user('id'),
								$this->data['Oauths'],
								$this->OauthServerRegistry->getDataSource()->connection);
			
			$this->set('consumer', $consumer);
		}
	}

/*
 * List all registered applications owned by the user
*/
	function index(){
		$apps = $this->OauthServerRegistry->find('all',
										array(
											'conditions' => array('osr_usa_id_ref' => $this->Auth->user('id')),
											'recursive' => -1));
		
		$this->set('apps', $apps);
	}

/*
 * List all registered applications
*/
	function admin_index(){
		$apps = $this->OauthServerRegistry->find('all',
										array(
											'recursive' => -1));
		
		$this->set('apps', $apps);
	}

/*
 * Edit a registered application owned by the user
*/
	function edit($appId = null){
		
		//http://twitter.com/oauth_clients/details/14895
		
		if($appId){
			//check if this app is belongs to this user
			//if the app is not belongs to the user log
		}
	}
}
?>
